﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cloud.Core.Data
{
    public class PageList<T> : List<T>
    {
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public int TotalRows { get; set; }
        public int TotalPages { get; set; }

        public PageList(IQueryable<T> query, int pageIndex, int pageSize)
        {
            this.TotalRows = query.Count();
            this.TotalPages = TotalRows % pageSize == 0 ? TotalRows / pageSize : TotalRows / pageSize + 1;
            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            var list = query.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
            this.AddRange(list);
        }
        public PageList(List<T> list, int pageIndex, int pageSize)
        {
            this.TotalRows = list.Count();
            this.TotalPages = TotalRows % pageSize == 0 ? TotalRows / pageSize : TotalRows / pageSize + 1;
            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            this.AddRange(list.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList());
        }

    }
}
